************************************************
************************************************
****the below syntax replicates and the analyses reported in the main text and appendix of
****Jane Green, Timothy Hellwig, Edward Fieldhouse. “Who Gets What: The Economy, Relative Gains, and Brexit.” forthcoming, BRITISH JOURNAL OF POLITICAL SCIENCE 
************************************************
************************************************
clear matrix
clear mata
set maxvar 30000
use "C:\BESabridged.dta"

graph set window fontface "Arial"

*economic perceptions
gen selfecon = selfEconW11
recode selfecon (9999=.)
gen localecon = localEconW11
recode localecon (9999=.)
gen regionecon = regionEconW11
recode regionecon (9999=.)
gen londonecon = londonEconW11
recode londonecon (9999=.)
gen ethnicecon = emEconW11 
recode ethnicecon (9999=.)
gen whitegbecon = wbEconW11
recode whitegbecon (9999=.)
gen wclassecon = wcEconW11
recode wclassecon (9999=.)
gen mclassecon = mcEconW11
recode mclassecon (9999=.)
*british identity scale
gen  britishnessw1101 =  (britishnessW11 - 1)/6
recode britishnessw1101 1000/max=.

/**********differences self v reference*********************/
/*9 point scales*/
gen selflocalecon = selfecon-localecon
gen selfregionecon = selfecon-regionecon
gen selflondonecon = selfecon-londonecon
gen selfethnicecon = selfecon-ethnicecon
gen selfwhitegbecon = selfecon-whitegbecon
gen selfwclassecon = selfecon-wclassecon
gen selfmclassecon =  selfecon-mclassecon
/*3 point scales*/
gen localecons = 0
recode localecons *=1 if selfecon>localecon
recode localecons *=-1 if selfecon<localecon
recode localecons *=. if selfecon==. | localecon==.
gen regionecons = 0
recode regionecons *=1 if selfecon>regionecon
recode regionecons *=-1 if selfecon<regionecon
recode regionecons *=. if selfecon==. | regionecon==.
gen londonecons = 0
recode londonecons *=1 if selfecon>londonecon
recode londonecons *=-1 if selfecon<londonecon
recode londonecons *=. if selfecon==. | londonecon==.
gen ethnicecons = 0
recode ethnicecons *=1 if selfecon>ethnicecon
recode ethnicecons *=-1 if selfecon<ethnicecon
recode ethnicecons *=. if selfecon==. | ethnicecon==.
gen whitegbecons = 0
recode whitegbecons *=1 if selfecon>whitegbecon
recode whitegbecons *=-1 if selfecon<whitegbecon
recode whitegbecons *=. if selfecon==. | whitegbecon==.
gen wclassecons = 0
recode wclassecons *=1 if selfecon>wclassecon
recode wclassecons *=-1 if selfecon<wclassecon
recode wclassecons *=. if selfecon==. | wclassecon==.
gen mclassecons = 0
recode mclassecons *=1 if selfecon>mclassecon
recode mclassecons *=-1 if selfecon<mclassecon
recode mclassecons *=. if selfecon==. | mclassecon==.

/**********differences self v reference*********************/
/*9 point scales*/
gen Londonvregion = londonecon-regionecon if gorW11 !=7
replace Londonvregion =0 if gorW11 ==7
gen whitevminority = ethnicecon-whitegbecon
replace whitevminority = whitegbecon-ethnicecon if (profile_ethnicity >=2 & profile_ethnicity <=15)
replace whitevminority=. if (profile_ethnicity ==. | profile_ethnicity >=16)
gen middlevworking= mclassecon-wclassecon 
replace middlevworking= mclassecon-wclassecon
/*3 point scales*/
gen localecongp = 0
recode localecongp *=1 if localecon>londonecon
recode localecongp *=-1 if londonecon>localecon
recode localecongp *=. if localecon==. | londonecon==.
gen regionecongp = 0
recode regionecongp *=1 if regionecon>londonecon
recode regionecongp *=-1 if londonecon>regionecon
recode regionecongp *=. if regionecon==. | londonecon==.
gen ethnicecongp = 0
recode ethnicecongp *=1 if whitegbecon>ethnicecon
recode ethnicecongp *=-1 if ethnicecon>whitegbecon
recode ethnicecongp *=. if whitegbecon==. | ethnicecon==.
gen wclassecongp = 0
recode wclassecongp *=1 if wclassecon>mclassecon
recode wclassecongp *=-1 if mclassecon>wclassecon
recode wclassecongp *=. if wclassecon==. | mclassecon==.

/*************attachment to in-group*****************/
gen regionbelong = belongGroup_1W11
recode regionbelong (9999=0)
gen localbelong = belongGroup_2W11
recode localbelong (9999=0)
gen mclassbelong = belongGroup_3W11
recode mclassbelong (9999=0)
gen wclassbelong = belongGroup_4W11
recode wclassbelong (9999=0)
gen ethnicbelong = belongGroup_5W11
recode ethnicbelong (9999=0)

/***********dependent variables********************/
/*primary DV: "If there was another referendum on EU membership, how do you think you would vote?"*/
recode euRefVoteW11 0=0 1=1 else=., into(eurefvotew11)
label define eurefvote 0 "remain" 1 "leave"
label values eurefvotew11 eurefvotew11
recode euRefVoteW13 0=0 1=1 else=., into(eurefvotew13)
label define eurefvotew13 0 "remain" 1 "leave"
label values eurefvotew13 eurefvotew13
recode euRefVoteW14 0=0 1=1 else=., into(eurefvotew14)
label define eurefvotew14 0 "remain" 1 "leave"
label values eurefvotew14 eurefvotew14
recode euRefVoteW15 0=0 1=1 else=., into(eurefvotew15)
label define eurefvotew15 0 "remain" 1 "leave"
label values eurefvotew15 eurefvotew15
recode profile_eurefvote 0=0 1=1 else=., into(eurefvote_p)
label define eurefvote_p 0 "remain" 1 "leave"
label values eurefvote_p eurefvote_p
/*ancillary DV: check models with linear var: how happy or disappointed are you that Britain voted to leave the EU?*/
gen EUscale = happyEULeaveW11
recode EUscale (9999=5)
/*ancillary DV: populism factor score*/
gen pop1 = populism1W11
recode pop1 9999=3
gen pop2 = populism2W11
recode pop2 9999=3
gen pop4 = populism4W11
recode pop4 9999=3
gen pop5 = populism5W11
recode pop5 9999=3
gen pop6 = populism6W11
recode pop6 9999=3
sum pop1 pop2 pop4 pop5 pop6
alpha pop1 pop2 pop4 pop5 pop6
factor pop1 pop2 pop4 pop5 pop6, pcf
rotate
predict populism
label variable populism "factor score of populismXW11; X 1,2,4,5,6"

/***********control variables*********************/
gen female = gender
recode female 1=0 2=1
gen income = profile_gross_householdW11
recode income (16/max=.)
gen natecon = econGenRetroW11
recode natecon (9999=.)
gen nateconw15 = econGenRetroW15
recode nateconw15 (9999=.)
gen immigculturalw7810 = immigCulturalW7
recode immigculturalw7810 9999=.
recode immigculturalw7810 .=1 if immigCulturalW8==1
recode immigculturalw7810 .=2 if immigCulturalW8==2
recode immigculturalw7810 .=3 if immigCulturalW8==3
recode immigculturalw7810 .=4 if immigCulturalW8==4
recode immigculturalw7810 .=5 if immigCulturalW8==5
recode immigculturalw7810 .=6 if immigCulturalW8==6
recode immigculturalw7810 .=7 if immigCulturalW8==7
recode immigculturalw7810 .=1 if immigCulturalW10==1
recode immigculturalw7810 .=2 if immigCulturalW10==2
recode immigculturalw7810 .=3 if immigCulturalW10==3
recode immigculturalw7810 .=4 if immigCulturalW10==4
recode immigculturalw7810 .=5 if immigCulturalW10==5
recode immigculturalw7810 .=6 if immigCulturalW10==6
recode immigculturalw7810 .=7 if immigCulturalW10==7
recode immigculturalw7810 1=7 2=6 3=5 4=4 5=3 6=2 7=1
gen immigcult7810 = (immigculturalw7810-1)/6
gen immigculturalw10 = immigCulturalW10
recode immigculturalw10 9999=. 1=7 2=6 3=5 4=4 5=3 6=2 7=1
gen immigcultural01 = (immigculturalw10-1)/6
gen immigculturalw13 = immigCulturalW13
recode immigculturalw13 9999=. 1=7 2=6 3=5 4=4 5=3 6=2 7=1
gen immigself01 = immigSelfW10
recode immigself01 9999=. 0=1 1=.9 2=.8 3=.7 4=.6 5=.5 6=.4 7=.3 8=.2 9=.1 10=0
gen immigcult = immigCulturalW10
recode immigcult 9999=. 1=6 2=5 3=4 4=3 5=2 6=1 7=0
gen immigcult01 = immigcult/6
gen immigecon = immigEconW10
recode immigecon 9999=. 1=6 2=5 3=4 4=3 5=2 6=1 7=0
gen immigecon01 = immigecon/6
gen immigcomb = (immigcult01 + immigecon01)/2
gen immigcultw15 = immigCulturalW15
recode immigcultw15 9999=. 1=6 2=5 3=4 4=3 5=2 6=1 7=0
gen immigcultw1501 = immigcultw15/6
gen immigeconw15 = immigEconW15
recode immigeconw15 9999=. 1=6 2=5 3=4 4=3 5=2 6=1 7=0
gen immigeconw1501 = immigeconw15/6
gen immigcombw1501 = (immigcultw1501 + immigeconw1501)/2
gen immigcombw15 = (immigcultw1501 + immigeconw1501)/2

/*authoritarianism measure from earlier waves*/
gen authorW10W11W12 = al_scaleW10W11W12
recode authorW10W11W12 *=0 if al_scaleW7W8W9~=.
gen authorW7W8W9 = al_scaleW7W8W9
recode authorW7W8W9 .=0
gen author = authorW7W8W9 + authorW10W11W12
recode author *=0 if al_scaleW7W8W9==0
gen author01 = author/10
/*creating ethnocentrism scale - dropping missings and reverse coding...*/
gen ethno1w7 = ethno1W7
recode ethno1w7 5=1 4=2 2=4 1=5 9999=.
gen ethno2w7 = ethno2W10
recode ethno2w7 9999=.
gen ethno3w7 = ethno3W10
recode ethno3w7 5=1 4=2 2=4 1=5 9999=.
gen ethno4w7 = ethno4W10
recode ethno4w7 9999=.
gen ethno5w7 = ethno5W10
recode ethno5w7 9999=.
pwcorr ethno1w7 ethno2w7 ethno3w7 ethno4w7 ethno5w7
gen ethnocentw7 = (((ethno1w7 + ethno2w7 + ethno3w7 + ethno4w7 + ethno5w7)/5)-1)/4
gen ethno1w10 = ethno1W10
recode ethno1w10 5=1 4=2 2=4 1=5 9999=.
gen ethno2w10 = ethno2W10
recode ethno2w10 9999=.
gen ethno3w10 = ethno3W10
recode ethno3w10 5=1 4=2 2=4 1=5 9999=.
gen ethno4w10 = ethno4W10
recode ethno4w10 9999=.
gen ethno5w10 = ethno5W10
recode ethno5w10 9999=.
pwcorr ethno1w10 ethno2w10 ethno3w10 ethno4w10 ethno5w10
gen ethnocentw10 = (((ethno1w10 + ethno2w10 + ethno3w10 + ethno4w10 + ethno5w10)/5)-1)/4
gen ec7nm = ethnocentw7
recode ec7nm .=0
gen ec10nm = ethnocentw10
recode ec10nm .=0
recode ec10nm *=0 if ethnocentw7~=.
gen ethnocent = ec7nm + ec10nm
/*creating discrimination scale*/
gen discrimBMEw10 = discrimBMEW10
recode discrimBMEw10 9999=.
gen discrimChristiansw10 = discrimChristiansW10
recode discrimChristiansw10 9999=.
gen discrimMenw10 = discrimMenW10
recode discrimMenw10 9999=.
gen discrimMuslimsw10 = discrimMuslimsW10
recode discrimMuslimsw10 9999=.
gen discrimWhitew10 = discrimWhiteW10
recode discrimWhitew10 9999=.
gen discrimWomenw10 = discrimWomenW10
recode discrimWomenw10 9999=.
alpha discrimBMEw10 discrimChristiansw10 discrimMenw10 discrimMuslimsw10 discrimWhitew10 discrimWomenw10
factor discrimBMEw10 discrimChristiansw10 discrimMenw10 discrimMuslimsw10 discrimWhitew10 discrimWomenw10, pcf
rotate
predict discrimination
label variable discrimination "factor score of discrimXW10"
gen discrimBMEw1001 = discrimBMEw10/10

/**********sample variables****************/
gen notlondon = 1
recode notlondon *=0 if gorW11==7
recode notlondon *=. if gorW11==.
gen ethnicity= profile_ethnicity
recode ethnicity 1=1 .=. *=0
*need to add wave 11 topups for subjective class
gen wclass = subjClassW12W14
recode wclass (2=1) (else=0)
recode wclass *=1 if subjClassW10W11==2
gen mclass = subjClassW12W14
recode mclass (1=1) (else=0)
recode mclass *=1 if subjClassW10W11==1

/**********crosstabs**********/
gen natecon3 = natecon
recode natecon3 1=-1 2=-1 3=0 4=1 5=1
gen selfecon3 = selfecon
recode selfecon3 1=-1 2=-1 3=0 4=1 5=1
gen whitegbecon3 = whitegbecon
recode whitegbecon3 1=-1 2=-1 3=0 4=1 5=1
gen ethnicecon3 = ethnicecon
recode ethnicecon3 1=-1 2=-1 3=0 4=1 5=1
tab2 ethnicecon3 selfecon3 if ethnicity==1, col chi 
tab2 ethnicecon3 whitegbecon3 if ethnicity==1, col chi 
gen eurefvotew9 = euRefVoteW9
recode eurefvotew9 (9999=.)
tab2 eurefvotew9 eurefvotew11, col
gen londonecon3 = londonecon
recode londonecon3 1=-1 2=-1 3=0 4=1 5=1
gen localecon3 = localecon
recode localecon3 1=-1 2=-1 3=0 4=1 5=1

/*setting Xs to common 0-1 scale*/
gen natecon01 = (natecon-1)/4
gen nateconw1501 = (nateconw15-1)/4
gen selfecon01 = (selfecon-1)/4
gen whitegbecon01 = (whitegbecon-1)/4
gen ethnicecon01 = (ethnicecon-1)/4
gen regionecon01 = (regionecon-1)/4
gen londonecon01 = (londonecon-1)/4
gen localecon01 = (localecon-1)/4
gen age01 = (ageW11-18)/74
gen edlevel01 = edlevelW11/5
gen income01 = (income-1)/14
gen self_ethnic = selfecon01-ethnicecon01
gen whitegb_ethnic = whitegbecon01-ethnicecon01
gen self_london = selfecon01-londonecon01
gen local_london = localecon01-londonecon01
gen self_nation = selfecon01-natecon01
label variable natecon01 "nation"
label variable selfecon01 "self"
label variable whitegbecon01 "white British"
label variable ethnicecon01 "ethnic minority"
label variable localecon01 "local community"
label variable londonecon01 "London"

/***variables for panels*/
gen lonb_locb = 0
recode lonb_locb *=1 if localecon01>.5 & londonecon01>.5
gen lonb_locw = 0
recode lonb_locw *=1 if localecon01>.5 & londonecon01<.5
gen lonw_locb = 0
recode lonw_locb *=1 if localecon01<.5 & londonecon01>.5
gen lonw_locw = 0
recode lonw_locw *=1 if localecon01<.5 & londonecon01<.5
recode lonb_locb *=. if localecon01==.
recode lonb_locw *=. if localecon01==.
recode lonw_locb *=. if localecon01==.
recode lonw_locw *=. if localecon01==.
gen wgbb_emib = 0
recode wgbb_emib *=1 if whitegbecon01>.5 & ethnicecon01>.5
gen wgbb_emiw = 0
recode wgbb_emiw *=1 if whitegbecon01>.5 & ethnicecon01<.5
gen wgbw_emib = 0
recode wgbw_emib *=1 if whitegbecon01<.5 & ethnicecon01>.5
gen wgbw_emiw = 0
recode wgbw_emiw *=1 if whitegbecon01<.5 & ethnicecon01<.5
recode wgbb_emib *=. if whitegbecon01==.
recode wgbb_emiw *=. if whitegbecon01==.
recode wgbw_emib *=. if whitegbecon01==.
recode wgbw_emiw *=. if whitegbecon01==.
gen wgbemicat = 0
recode wgbemicat *=1 if wgbb_emib==1
recode wgbemicat *=2 if wgbb_emiw==1
recode wgbemicat *=3 if wgbw_emib==1
recode wgbemicat *=4 if wgbw_emiw==1

*******************************************
******DESCRIPTIVE STATISTICS***************
*******************************************
sum eurefvotew11 selfecon01 natecon01 whitegbecon01 ethnicecon01 self_ethnic whitegb_ethnic localecon01 londonecon01 self_london local_london immigcomb ethnocent author01 age01 female edlevel01 income01 ethnicity if wave11==1

/*****************FIGURE 1****************/
ciplot natecon01 selfecon01 whitegbecon01 ethnicecon01 if (ethnicity==1), horizontal scheme(s1mono) xtitle("perceptions of group economy, from 0 (worse) to 1 (better)")

/*****************TABLE 1****************/
sum id if ethnicecon3==-1 & selfecon3==-1 & ethnicity==1
sum id if ethnicecon3==-1 & selfecon3==0 & ethnicity==1
sum id if ethnicecon3==-1 & selfecon3==1 & ethnicity==1
sum id if ethnicecon3==0 & selfecon3==-1 & ethnicity==1
sum id if ethnicecon3==0 & selfecon3==0 & ethnicity==1
sum id if ethnicecon3==0 & selfecon3==1 & ethnicity==1
sum id if ethnicecon3==1 & selfecon3==-1 & ethnicity==1
sum id if ethnicecon3==1 & selfecon3==0 & ethnicity==1
sum id if ethnicecon3==1 & selfecon3==1 & ethnicity==1
sum id if ethnicecon3==-1 & whitegbecon3==-1 & ethnicity==1
sum id if ethnicecon3==-1 & whitegbecon3==0 & ethnicity==1
sum id if ethnicecon3==-1 & whitegbecon3==1 & ethnicity==1
sum id if ethnicecon3==0 & whitegbecon3==-1 & ethnicity==1
sum id if ethnicecon3==0 & whitegbecon3==0 & ethnicity==1
sum id if ethnicecon3==0 & whitegbecon3==1 & ethnicity==1
sum id if ethnicecon3==1 & whitegbecon3==-1 & ethnicity==1
sum id if ethnicecon3==1 & whitegbecon3==0 & ethnicity==1
sum id if ethnicecon3==1 & whitegbecon3==1 & ethnicity==1

*setting a sample
qui logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 selfecon01 whitegbecon01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
predict e if e(sample)
gen sample = e
recode sample .=0 *=1
*Base: White respondents: white British

/**************TABLE 2 & FIGURE 2********************/
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 selfecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 selfecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(self, replace) title("A. Self") nodraw
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegbecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(ingroup, replace) title("B. Ethnic In-Group") nodraw
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 ethnicecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(outgroup, replace) title("C. Ethnic Out-Group") nodraw
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1)) atmean
marginsplot, scheme(s1mono) name(invout, replace) title("D. In-Group vs Out-Group") nodraw
graph combine self ingroup outgroup invout, scheme(s1mono) ycommon
graph save "C:\figure2.gph", replace

/******************FIGURES 3 & 4*******************/
/*see Table A4 below*/

/*****************TABLE 3****************/
sum id if londonecon3==-1 & selfecon3==-1 & notlondon==1
sum id if londonecon3==-1 & selfecon3==0 & notlondon==1
sum id if londonecon3==-1 & selfecon3==1 & notlondon==1
sum id if londonecon3==0 & selfecon3==-1 & notlondon==1
sum id if londonecon3==0 & selfecon3==0 & notlondon==1
sum id if londonecon3==0 & selfecon3==1 & notlondon==1
sum id if londonecon3==1 & selfecon3==-1 & notlondon==1
sum id if londonecon3==1 & selfecon3==0 & notlondon==1
sum id if londonecon3==1 & selfecon3==1 & notlondon==1
sum id if londonecon3==-1 & localecon3==-1 & notlondon==1
sum id if londonecon3==-1 & localecon3==0 & notlondon==1
sum id if londonecon3==-1 & localecon3==1 & notlondon==1
sum id if londonecon3==0 & localecon3==-1 & notlondon==1
sum id if londonecon3==0 & localecon3==0 & notlondon==1
sum id if londonecon3==0 & localecon3==1 & notlondon==1
sum id if londonecon3==1 & localecon3==-1 & notlondon==1
sum id if londonecon3==1 & localecon3==0 & notlondon==1
sum id if londonecon3==1 & localecon3==1 & notlondon==1

/*****************TABLE 4********************/
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 selfecon01 if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 localecon01 if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 londonecon01 if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 localecon01 londonecon01 if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 local_london if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic natecon01 lonb_locb lonw_locb lonb_locw lonw_locw if notlondon==1 & ethnicity==1 [pweight=weight_w10w11]

/******************TABLE 5********************/
margins, at(whitegb_ethnic=0 natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 lonb_locb=1 lonw_locb=0 lonb_locw=0 lonw_locw=0) atmean
margins, at(whitegb_ethnic=0 natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 lonb_locb=0 lonw_locb=1 lonb_locw=0 lonw_locw=0) atmean
margins, at(whitegb_ethnic=0 natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 lonb_locb=0 lonw_locb=0 lonb_locw=1 lonw_locw=0) atmean
margins, at(whitegb_ethnic=0 natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 lonb_locb=0 lonw_locb=0 lonb_locw=0 lonw_locw=1) atmean

/*****************TABLE A1*********************/
/*variable descriptions*/

/*****************TABLE A2*********************/
sum eurefvotew11 eurefvote_p selfecon01 natecon01 whitegbecon01 ethnicecon01 self_ethnic whitegb_ethnic immigcult7810 ethnocent author01 age01 female edlevel01 income01 ethnicity EUscale populism  localecon01 londonecon01 local_london notlondon if wave11==1

/*****************TABLE A3*********************/
logit eurefvotew11 natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 selfecon01 if ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 if ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
logit eurefvotew11 natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic if ethnicity==1 [pweight=weight_w10w11]

/**************TABLE A4 & FIGURE 3 & FIGURE 4***********************/
logit eurefvotew11 eurefvote_p natecon01 ethnocent author01 age01 female edlevel01 income01 c.whitegb_ethnic##c.immigcult7810 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1) immigcult7810=(0 1)) atmean
marginsplot, scheme(s1mono) name(immig, replace) title("") 
graph save Graph "C:\figure3.gph", replace
logit eurefvotew11 eurefvote_p natecon01 c.immigcult7810 ethnocent author01 age01 female edlevel01 c.whitegb_ethnic##c.income01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 whitegb_ethnic=(-1(.5)1) income01=(.167 .673)) atmean
marginsplot, scheme(s1mono) name(income, replace) title("A. Household income") nodraw
logit eurefvotew11 eurefvote_p natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 c.whitegb_ethnic##c.britishnessw1101 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1) britishnessw1101=(0 1)) atmean
marginsplot, scheme(s1mono) name(identity, replace) title("B. British identity") nodraw
graph combine income identity, scheme(s1mono) ycommon
graph save Graph "C:\figure4.gph", replace

/**************TABLE A5 - panel analysis*******************/
reg immigcultw1501 immigcult7810 whitegb_ethnic if ethnicity==1 [pweight=weight_w11w15]
reg immigcultw1501 immigcult7810 whitegb_ethnic natecon01 ethnocent author01 age01 female edlevel01 income01 if ethnicity==1 [pweight=weight_w11w15]

/**************TABLE A6 & FIGURE A4********************/
sort id
merge 1:1 id using "C:\randomisation.dta"
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 natecon01 selfecon01 c.whitegbecon01##c.EMeconbeforeWBecon if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegbecon01=(0(.25)1) EMeconbeforeWBecon=(0 1)) atmean
marginsplot, scheme(s1mono) name(ingroup, replace) title("A. In-Group") nodraw
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 natecon01 selfecon01 c.ethnicecon01##c.EMeconbeforeWBecon if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 ethnicecon01=(0(.25)1) EMeconbeforeWBecon=(0 1)) atmean
marginsplot, scheme(s1mono) name(outgroup, replace) title("B. Out-Group") nodraw
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 natecon01 c.self_ethnic##c.EMeconbeforeWBecon if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 self_ethnic=(-1(.5)1) EMeconbeforeWBecon=(0 1)) atmean
marginsplot, scheme(s1mono) name(selfvout, replace) title("C. Self vs Out-Group") nodraw
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 natecon01 c.whitegb_ethnic##c.EMeconbeforeWBecon if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1) EMeconbeforeWBecon=(0 1)) atmean
marginsplot, scheme(s1mono) name(invout, replace) title("D. In-Group vs Out-Group") nodraw
graph combine ingroup outgroup selfvout invout, scheme(s1mono) ycommon
graph save Graph "C:\figureA4.gph", replace

/**************TABLE A7 & FIGURE A5************************/
reg EUscale natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 selfecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 selfecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(self, replace) title("A. Self") nodraw
reg EUscale natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegbecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(ingroup, replace) title("B. In-Group") nodraw
reg EUscale natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 ethnicecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(outgroup, replace) title("C. Out-Group") nodraw
reg EUscale natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
reg EUscale natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1)) atmean
marginsplot, scheme(s1mono) name(invout, replace) title("D. In-Group vs Out-Group") nodraw
graph combine self ingroup outgroup invout, scheme(s1mono) ycommon
graph save ":\figureA5.gph", replace

/**************TABLE A8********************/
reg populism natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 selfecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 selfecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(self, replace) title("A. Self") nodraw
reg populism natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegbecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(ingroup, replace) title("B. In-Group") nodraw
reg populism natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 ethnicecon01=(0(.25)1)) atmean
marginsplot, scheme(s1mono) name(outgroup, replace) title("C. Out-Group") nodraw
reg populism natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegbecon01 ethnicecon01 if ethnicity==1 [pweight=weight_w10w11]
reg populism natecon01 immigcult7810 ethnocent author01 age01 female edlevel01 income01 whitegb_ethnic if ethnicity==1 [pweight=weight_w10w11]
quietly margins, at(natecon01=.5 immigcult7810=.5 ethnocent=.5 author01=.65 age01=.47 female=1 edlevel01=.6 income01=.357 whitegb_ethnic=(-1(.5)1)) atmean
marginsplot, scheme(s1mono) name(invout, replace) title("D. In-Group vs Out-Group") nodraw
graph combine self ingroup outgroup invout, scheme(s1mono) ycommon
graph save "C:\figureA6.gph", replace

/**************TABLE A9********************/
logit eurefvotew11 eurefvote_p immigcult7810 ethnocent author01 age01 female edlevel01 income01 natecon01 wgbb_emib wgbb_emiw wgbw_emib wgbw_emiw if ethnicity==1 [pweight=weight_w10w11]

/*****FIGURE A1*****/
gen econgenretrow7 = econGenRetroW7
recode econgenretrow7 (9999=.)
gen econgenretrow11 = econGenRetroW11
recode econgenretrow11 (9999=.)
gen econpersonalretrow7 = econPersonalRetroW7
recode econpersonalretrow7 (9999=.)
gen econpersonalretrow11 = econPersonalRetroW11
recode econpersonalretrow11 (9999=.)
gen econgrw701 = (econgenretrow7-1)/4
gen econgrw1101 = (econgenretrow11-1)/4
gen econprw701 = (econpersonalretrow7-1)/4
gen econprw1101 = (econpersonalretrow11-1)/4
ciplot econgrw701 econgrw1101 econprw701 econprw1101 if (eurefvote_p==1) & (wave7==1), horizontal scheme(s1mono) xtitle("Leavers' economic perceptions, from 0 (worse) to 1 (better)") xla(0(.25)1)
ciplot econgrw701 econgrw1101 econprw701 econprw1101 if (eurefvote_p==0) & (wave7==1), horizontal scheme(s1mono) xtitle("Remainers' economic perceptions, from 0 (worse) to 1 (better)") xla(0(.25)1)

/***************FIGURE A2******************/
ciplot whitegbecon01 if (ethnicity==1), by (EMeconbeforeWBecon) scheme(s1mono) name(means1, replace) title("A. White British") nodraw
ciplot ethnicecon01 if (ethnicity==1), by (EMeconbeforeWBecon) scheme(s1mono) name(means2, replace) title("B. Ethnic Minority Immigrants") nodraw
graph combine means1 means2, scheme(s1mono) ycommon
graph save Graph "C:\figureA2.gph", replace

/***************FIGURE A3******************/
by eurefvotew11, sort : ttest whitegb_ethnic if ethnicity==1, by(EMeconbeforeWBecon)
ciplot whitegb_ethnic if (ethnicity==1 & eurefvotew11==0), by (EMeconbeforeWBecon) scheme(s1mono) name(means3, replace) title("B. Remain Voters") nodraw
ciplot whitegb_ethnic if (ethnicity==1 & eurefvotew11==1), by (EMeconbeforeWBecon) scheme(s1mono) name(means4, replace) title("A. Leave Voters") nodraw
graph combine means4 means3, scheme(s1mono) ycommon
graph save Graph "C:\\figureA3.gph", replace
